import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material

// 独立的弹窗组件
Popup {
    id: promptPopup
    
    // 弹窗属性
    property string fieldLabel: ""
    property string currentValue: ""
    property int fieldIndex: -1  // 添加字段索引属性

    // 将弹窗的父级设置为应用窗口的覆盖层，以实现全局定位
    parent: ApplicationWindow.overlay

    // 将弹窗在窗口顶部居中显示
    x: (parent.width - width) / 2
    y: 50 // 距离顶部的距离，可以根据需要调整
    
    // 信号
    signal promptSelected(string selectedPrompt)
    signal popupClosed()

    // 弹窗配置
    modal: true
    focus: true
    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
    
    // 无边框背景
    background: Rectangle {
        color: "transparent"
        border.width: 0
    }
    
    // 根据字段标签获取对应的提示词列表
    function getPromptsForField(label) {
        var defaultPrompts = [];
        if (label.includes("人物") || label.includes("Character")) {
            defaultPrompts = [
                "beautiful girl",
                "handsome man",
                "cute child",
                "elderly person",
                "anime character",
                "realistic portrait",
                "1girl",
                "1boy",
                "multiple people"
            ]
        } else if (label.includes("镜头") || label.includes("Camera")) {
            defaultPrompts = [
                "close-up",
                "wide shot",
                "medium shot",
                "bird's eye view",
                "low angle",
                "high angle",
                "cinematic",
                "portrait",
                "full body"
            ]
        } else if (label.includes("服饰") || label.includes("Apparel")) {
            defaultPrompts = [
                "red dress",
                "leather jacket",
                "casual wear",
                "formal suit",
                "traditional costume",
                "modern outfit",
                "school uniform",
                "wedding dress"
            ]
        } else if (label.includes("表情") || label.includes("Expression")) {
            defaultPrompts = [
                "smiling",
                "sad",
                "angry",
                "surprised",
                "peaceful",
                "mysterious",
                "happy",
                "serious",
                "laughing"
            ]
        } else if (label.includes("动作") || label.includes("Action")) {
            defaultPrompts = [
                "running",
                "jumping",
                "dancing",
                "sitting",
                "standing",
                "walking",
                "flying",
                "fighting",
                "sleeping"
            ]
        } else if (label.includes("场景") || label.includes("Scene")) {
            defaultPrompts = [
                "in a forest",
                "on a beach",
                "in a city",
                "at sunset",
                "indoor studio",
                "mountain landscape",
                "underwater",
                "in space",
                "medieval castle"
            ]
        } else {
            defaultPrompts = [
                "masterpiece, best quality",
                "highly detailed",
                "photorealistic",
                "8k resolution",
                "professional lighting",
                "vibrant colors",
                "sharp focus",
                "cinematic composition",
                "dramatic lighting",
                "soft shadows"
            ]
        }

        var userPromptsMap = fileHelper.loadUserPrompts();
        var userPromptsForField = userPromptsMap[label] || [];

        // 合并并去重
        var allPrompts = defaultPrompts.concat(userPromptsForField);
        return allPrompts.filter((value, index, self) => self.indexOf(value) === index);
    }

    // 弹窗内容
    Rectangle {
        id: popupContent
        width: 400
        height: 500
        color: Material.backgroundColor
        radius: 8
        border.color: Material.accent
        border.width: 2
        
        ColumnLayout {
            anchors.fill: parent
            anchors.margins: 16
            spacing: 12
            
            // 标题栏
            RowLayout {
                Layout.fillWidth: true
                
                Label {
                    text: "常用提示词 - " + promptPopup.fieldLabel
                    font.bold: true
                    font.pointSize: 14
                    Layout.fillWidth: true
                }
                
                Button {
                    text: "×"
                    Layout.preferredWidth: 32
                    Layout.preferredHeight: 32
                    Material.background: Material.Red
                    Material.foreground: Material.White
                    onClicked: promptPopup.close()
                }
            }
            
            // 分割线
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 1
                color: Material.accent
            }
            
            // 新增：用于添加新提示词的输入框和按钮
            RowLayout {
                Layout.fillWidth: true;
                TextField {
                    id: newPromptInput
                    Layout.fillWidth: true
                    placeholderText: qsTr("添加新的提示词...")
                }
                Button {
                    text: qsTr("添加")
                    onClicked: {
                        if (newPromptInput.text.trim() !== "") {
                            fileHelper.saveUserPrompt(fieldLabel, newPromptInput.text.trim());
                            newPromptInput.text = "";
                            // 重新加载列表以显示新添加的项
                            promptsList.model = getPromptsForField(fieldLabel);
                        }
                    }
                }
            }

            // 当前值显示
            Label {
                text: "当前值: " + (promptPopup.currentValue || "(空)")
                font.italic: true
                color: Material.secondaryTextColor
                wrapMode: Text.WordWrap
                Layout.fillWidth: true
            }
            
            // 常用提示词列表
            ScrollView {
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: true
                
                ListView {
                    id: promptsList
                    model: getPromptsForField(fieldLabel)
                    spacing: 4
                    
                    delegate: Rectangle {
                        width: promptsList.width
                        height: 40
                        color: promptMouseArea.containsMouse ? Material.accent : "transparent"
                        radius: 4
                        border.color: Material.accent
                        border.width: promptMouseArea.containsMouse ? 1 : 0
                        
                        Label {
                            anchors.left: parent.left
                            anchors.right: parent.right
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.margins: 12
                            text: modelData
                            color: promptMouseArea.containsMouse ? Material.background : Material.foreground
                            elide: Text.ElideRight
                        }
                        
                        MouseArea {
                            id: promptMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: {
                                promptPopup.promptSelected(modelData)
                                // promptPopup.close()
                            }
                        }
                    }
                }
            }
            
            // 底部按钮
            RowLayout {
                Layout.fillWidth: true
                Layout.alignment: Qt.AlignRight
                spacing: 8

                Button {
                    text: qsTr("清空历史")
                    Material.background: Material.Red
                    onClicked: {
                        fileHelper.clearUserPrompts();
                        promptsList.model = getPromptsForField(fieldLabel);
                    }
                }
                
                Button {
                    text: "清空"
                    Material.background: Material.Orange
                    onClicked: {
                        promptPopup.promptSelected("")
                        promptPopup.close()
                    }
                }
                
                Button {
                    text: "取消"
                    Material.background: Material.Grey
                    onClicked: promptPopup.close()
                }
            }
        }
    }
    
    // 弹窗关闭时发送信号
    onClosed: {
        promptPopup.popupClosed()
    }


    
    // 打开弹窗的方法
    function openPopup(label, value) {
        fieldLabel = label
        currentValue = value
        open()
    }
}
